// 接口把所有的接口都放到api.js (request.js)中 

// ajax 二次封装:
// type 请求的方式  
// url  请求的地址
// params 携带对象此参数
function request(url, params = {}, type = "get") {   // type放到最后  主要是get方式请求时可以省略
    // 创建  =>   接收一个异步的操作(同步也行)  根据异步操作的结果改变状态
    var p = new Promise(function (resolve, reject) {
        $.ajax({
            type: type,
            url,
            // 如何让一个变量作为对象的键名  
            data: { ...params },
            dataType: "json",
            success: function (result) {
                resolve(result);
            }
        })
    })
    return p;
}


/* register */
const isExist = params => request("../php/isExist.php", params, "get");
const register = params => request("../php/register.php", params, "post");

/* login */
const login = params => request("../php/login.php", params, "post");
const loginAccounnt = params => request("../php/login_account.php", params, "post");

/* index */
const searchAllGradeOrderLimit = params => request("../php/searchAllGradeOrderLimit.php", params);
const deleteGradeById = params => request("../php/deleteGradeById.php", params);

//渲染二级导航
const searchNavListById = params => request("../php/searchNavListById.php", params);



// 列表页
const searchAllGoodsOrderLimit = params => request("../php/searchAllGoodsOrderLimit.php", params);

// 详情页
const searchGoodsById = params => request("../php/searchGoodsById.php", params);
const addToShoppingCar = params => request("../php/addToShoppingCar.php", params, "post");

// 购物车
const searchShoppingCarByUser = params => request("../php/searchShoppingCarByUser.php", params);


